using System;
using System.Collections.Generic;

namespace OpenRay
{
	//Immutable Class
	public class Primitive
	{	
		private readonly Matrix worldToModel;
		private readonly Matrix modelToWorld;
		
		private readonly List<Light> lights = new List<Light>();
		private readonly Geometry geometry;
		private readonly bool localViewer = true;
		
		public Primitive( Matrix m2w, Matrix w2m, Geometry g, IList<Light> lts )
		{
			worldToModel = w2m;
			modelToWorld = m2w;
			geometry = g;
			
			foreach ( Light l in lts )
			{
				lights.Add( l );	
			}
		}
		
		public void Trace(Ray r, uint depth, IList<PrimitiveHit> ret) {
			IList<GeometryHit> hits = geometry.Trace(worldToModel * r);
			
			for( int i = 0; i < hits.Count; i++  )
			{
				ret.Add( new PrimitiveHit( hits[i], lights, worldToModel, localViewer, depth - 1 ) );
			}
		}
		
		public BoundingBox BoundingBox()
		{
			return modelToWorld * geometry.BoundingBox();			
		}
	}
}

